package mailgod;

import processing.core.*;

public class GridCoordinates {
	  GridLine parent;
	  float[][] planeArray;
	  float[][] spaceArray;
	  PVector[] pointArray;
	  PVector[] spacePointArray;
	  int[] shapeArray;
	  int shapeInd;

	  public GridCoordinates(float[][] thePlaneArray, float[][] theSpaceArray, PVector[] thePointArray, PVector[] theSpacePointArray, int[] theShapeArray, int theShapeInd, GridLine theParent ) {
	  planeArray = thePlaneArray;
	  spaceArray = theSpaceArray;
	  pointArray = thePointArray;
	  spacePointArray = theSpacePointArray;
	  shapeArray = theShapeArray;
	  shapeInd = theShapeInd;
	  parent = theParent;
	  setCoords ();
	  }

	public void setCoords () {
	    float myRatio;
	    int count = planeArray.length;
	    for (int i = 0; i < count; i++) {
	      int j = (i+1)%count;
	      int minInd = i;
	      int maxInd = j;
	      if (spaceArray[i][parent.plane] >= spaceArray[j][parent.plane]) {
	        minInd = j;
	        maxInd = i;
	      }

	      if ((parent.planeValue >= spaceArray[minInd][parent.plane]) && (parent.planeValue <= spaceArray[maxInd][parent.plane])) {
	        if ((spaceArray[maxInd][parent.plane]-spaceArray[minInd][parent.plane])==0f) {
	        myRatio = 0;
	        } else {
	        myRatio = (parent.planeValue-spaceArray[minInd][parent.plane])/(spaceArray[maxInd][parent.plane]-spaceArray[minInd][parent.plane]);
	        };
	        if (pointArray == null) {
	          pointArray = new PVector[1];
	          spacePointArray = new PVector[1];
	          shapeArray = new int[1];
	        } 
	        else {
	          pointArray = (PVector[]) PApplet.expand(pointArray,pointArray.length+1);
	          spacePointArray = (PVector[]) PApplet.expand(spacePointArray,spacePointArray.length+1);
	          shapeArray =  PApplet.expand(shapeArray,shapeArray.length+1);
	        }
	        pointArray[pointArray.length-1] = new PVector((planeArray[minInd][0]*(1-myRatio)+planeArray[maxInd][0]*myRatio),(planeArray[minInd][1]*(1-myRatio)+planeArray[maxInd][1]*myRatio),0); 
	        spacePointArray[spacePointArray.length-1] = new PVector((spaceArray[minInd][0]*(1-myRatio)+spaceArray[maxInd][0]*myRatio),(spaceArray[minInd][1]*(1-myRatio)+spaceArray[maxInd][1]*myRatio),(spaceArray[minInd][2]*(1-myRatio)+spaceArray[maxInd][2]*myRatio)); 
	        shapeArray[shapeArray.length-1] = shapeInd;
	        //println(spacePointArray[pointArray.length-1]);
	        //println(shapeArray[shapeArray.length-1]);
	        //println(set0_Shape[0]);
	      }
	    }
	  }

	public PVector[] getPointArray() {
	  return pointArray;  
	}

	public PVector[] getSpacePointArray() {
	  return spacePointArray;  
	}

	public int[] getShapeArray() {
	  return shapeArray;  
	}
}